package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Patient;
import com.jrtc.base.entity.bo.PatientRecordcase;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.vo.PatientRecordcaseVo;
import com.jrtc.service.PatientRecordcaseService;
import com.jrtc.service.PatientService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 患者病历表(PatientRecordcase)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("patientRecordcase")
public class PatientRecordcaseController {
    /**
     * 服务对象
     */
    @Resource
    private PatientRecordcaseService patientRecordcaseService;
    @Resource
    private PatientService patientService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public PatientRecordcase selectOne(Integer id) {
        return this.patientRecordcaseService.queryById(id);
    }
    
    @RequestMapping("getPatientRecordcaseList")
    public ResultDTO getPatientRecordcaseList(@RequestBody PatientRecordcase patientRecordcase)
    {
        IPage<PatientRecordcase> ipage = patientRecordcaseService.queryPatientRecordcaseList(patientRecordcase);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertPatientRecordcase")
    public ResultDTO insertPatientRecordcase(@RequestBody PatientRecordcase patientRecordcase)
    {
        PatientRecordcase tmpPatientRecordcase = patientRecordcaseService.insert(patientRecordcase);
        if (tmpPatientRecordcase != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getPatientRecordcaseById")
    public ResultDTO getPatientRecordcaseById(@RequestBody Map<String,Integer> map)
    {
        int patientRecordcaseId = map.get("patientRecordcaseId");
        PatientRecordcase patientRecordcase = patientRecordcaseService.queryById(patientRecordcaseId);
        if (patientRecordcase==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(patientRecordcase);
    }

    @RequestMapping("updatePatientRecordcase")
    public ResultDTO updatePatientRecordcase(@RequestBody PatientRecordcase patientRecordcase)
    {
        if (patientRecordcase==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (patientRecordcaseService.update(patientRecordcase)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deletePatientRecordcase")
    public ResultDTO deletePatientRecordcase(@RequestBody Map<String,Integer> map)
    {
        int patientRecordcaseId = map.get("patientRecordcaseId");
        if (patientRecordcaseService.deleteById(patientRecordcaseId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 创建病例
     * */
    @RequestMapping("createCase")
    public ResultDTO createCase(HttpServletRequest request, @RequestBody PatientRecordcaseVo patientRecordcaseVo){
        if (patientRecordcaseVo.getPatientId() == 0){
            Patient patient;
            try {
                patient = patientService.getLoginPatient(request);
            } catch (Exception e){
                return null;
            }
            patientRecordcaseVo.setPatientId(patient.getPatientId());
        }

        if (patientRecordcaseVo != null){
            int aCase;
            try {
                aCase = patientRecordcaseService.createCase(patientRecordcaseVo);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }
            if (aCase > 0){
                return ResultDTOBuilder.success("添加成功");
            }
            else {
                return ResultDTOBuilder.failureMsg("添加失败");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }
}